/**
 * Created by chenzheyu on 2017/4/11.
 */
$(function () {
    var limit = 20;
    var SEOVal = '';

    function xin() {
        $.ajax({
            url: ctx + '/rentorder/selectRentOrdersCount',
            type: 'GET',
            success: function (data) {

                var text = data.split(',');

                var li=$('.CustomTabs ul li');

                if(text[0]>0){
                    if(li.eq(0).find('.CustomBadge').length>0){
                        li.eq(0).find('.CustomBadge').remove();
                        li.eq(0).Badge({data:{text:text[0]>99?'99+':text[0],state:'No'}})
                    }else {
                        li.eq(0).Badge({data:{text:text[0]>99?'99+':text[0],state:'No'}})
                    }
                }

                if(text[1]>0){
                    if(li.eq(7).find('.CustomBadge').length>0){
                        li.eq(7).find('.CustomBadge').remove();
                        li.eq(7).Badge({data:{text:text[1]>99?'99+':text[1],state:'No'}})
                    }else {
                        li.eq(7).Badge({data:{text:text[1]>99?'99+':text[1],state:'No'}})
                    }
                }

                if(text[2]>0){
                    if(li.eq(8).find('.CustomBadge').length>0){
                        li.eq(8).find('.CustomBadge').remove();
                        li.eq(8).Badge({data:{text:text[2]>99?'99+':text[2],state:'No'}})
                    }else {
                        li.eq(8).Badge({data:{text:text[2]>99?'99+':text[2],state:'No'}})
                    }
                }
            }
        })
    }
    xin();
    setInterval(function () {
        xin()
    }, 60000);


    CompletedOrder(1, SEOVal);
    function CompletedOrder(index, inquireVal) {
        var ConfirmedOrderOptin = {
            plateNoOrPassengerPhone: inquireVal || '',
            status: 45,
            "limit": 20,
            'pager.offset': (index - 1) * limit
        };
        $.ajax({
            url: ctx + '/rentorder/selectRentOrders',
            data: ConfirmedOrderOptin,
            type: 'GET',
            success: function (data) {
                ;
                var option = {
                    data: {
                        Thead: {
                            PullDownAction: '操作',
                            OrderNumber: "单号",
                            totalFree:'总金额',
                            payed: '租金',
                            Store: "门店",
                            RetrievingMeans: "取车地址",
                            ReturnType: "还车地址",
                            HireTime: "还车时间",
                            ReturnTime: "续租时间",
                            reReturnTime: "剩余时间",
                            ClientName: "客户名称",
                            ClientPhone: "客户电话",
                            Car: "车辆号牌",
                            CarType: "车辆类型",
                            // RentalStandard: "租金标准",
                            Prepayment: "租车押金",
                            ViolationMoneys: "违章押金",
                            // PrepayMoney: "预授金额",
                            SubordinateCompanies: "所属公司",
                            Remark: "备注"
                        },
                        Tbody: []
                    }
                };
                var PresentTimestamp = Date.parse((new Date()) / 1000);
                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var val = {
                            id: data.content[index].id,
                            PullDownAction: {
                                Default:{
                                    name: '查看',
                                    action: function (e) {
                                        window.location.href = ctx + '/rentorder/orderinfo?id=' + data.content[index].id;
                                    }
                                },
                                data: [
                                ]
                            },
                            OrderNumber: data.content[index].orderNo,
                            totalFree:data.content[i].totalFee,
                            payed: '',
                            Store: data.content[index].shopName,
                            RetrievingMeans: data.content[index].takeCarWay == 1 ? data.content[index].startAddress : "门店自取",
                            ReturnType: data.content[index].backCarWay == 1 ? data.content[index].endAddress : '门店自还',
                            HireTime: $().Timestamp.TimestampDate(data.content[index].rentTime),
                            ReturnTime: data.content[index].reRentTime ? $().Timestamp.TimestampDate(data.content[index].reRentTime) : '无续租',
                            ResidueTime: $().Timestamp.DayCalculate(data.content[index].reRentTime ? data.content[index].reRentTime - PresentTimestamp : data.content[index].rentTime - PresentTimestamp),
                            ClientName: data.content[index].passengerName,
                            ClientPhone: data.content[index].passengerPhone,
                            Car: data.content[index].plateNo,
                            CarType: data.content[index].category,
                            // RentalStandard: data.content[index].RentalStandard,
                            Prepayment: data.content[index].prepayRent,
                            ViolationMoneys: data.content[index].peccancyForegift,
                            // PrepayMoney: data.content[index].PrepayMoney,
                            SubordinateCompanies: data.content[index].companyName,
                            Remark: data.content[index].memo
                        };

                        switch (data.content[i].isPay) {
                            case 0:
                                val.payed = $('<span class="badge">未交付</span>');
                                break;
                            case 1:
                                val.payed = data.content[i].payed;
                                break;
                            case 2:
                                val.payed =data.content[i].payed;
                                break;
                            case 3:
                                val.payed = data.content[i].payed;
                                break;
                            case 4:
                                val.payed = '已结算';
                                break;
                        }

                        console.log(data.content[i].isBackPrepayRent)
                        console.log(data.content[i].isBackPeccancyForegift)
                        switch (data.content[i].isBackPrepayRent) {
                            case 0:
                                val.Prepayment = '未交付';
                                break;
                            case 1:
                                val.Prepayment = data.content[i].prepayRent;
                                val.PullDownAction.data.push({
                                    name: "退还押金",
                                    action: function (e) {
                                        var SingID = e.parent().parent().parent().parent().data().id;
                                        var SinglePin = {
                                            data: {
                                                Title: '退还押金',
                                                Content: "是否退还押金?"
                                                ,
                                                OkChange: function () {
                                                    var Single = {
                                                        id: SingID,
                                                        type: 0
                                                    };
                                                    $.ajax({
                                                        url: ctx + '/rentorder/backForegift',
                                                        data: Single,
                                                        type: 'POST',
                                                        success: function (data, status) {
                                                            if (status == "success") {
                                                                new $().Overlays({
                                                                    data: {
                                                                        text: '违章预售金退还成功！',
                                                                        state: 'Ok'
                                                                    }
                                                                });
                                                            }
                                                            if (status == "error") {
                                                                new $().Overlays({
                                                                    data: {
                                                                        text: '违章预售金退还失败！',
                                                                        state: 'On'
                                                                    }
                                                                });
                                                            }
                                                            ajaxFreshOrder(1)
                                                        }
                                                    })

                                                }
                                            }
                                        };
                                        $('body').ModalBox(SinglePin);
                                    }
                                });
                                break;
                            case 2:
                                val.Prepayment = '已退还';
                                break;
                        }
                        switch (data.content[i].isBackPeccancyForegift) {
                            case 0:
                                val.ViolationMoneys = '未交付';
                                break;
                            case 1:
                                val.ViolationMoneys = data.content[i].peccancyForegift;
                                break;
                            case 2:
                                val.ViolationMoneys = '已退还';
                                break;
                        }


                        option.data.Tbody.push(val)
                    })()

                }
                var PagingMdOption = {
                    data: {
                        rows: 20,
                        nowrows: index,
                        Pages: '',
                        total: data.totalElements,
                        change: function (index) {
                            CompletedOrder(index);
                        }
                    }
                };
                PagingMdOption.data.Pages = Math.ceil((PagingMdOption.data.total / PagingMdOption.data.rows));

                var CompletedContent = $('.CompletedContent');
                CompletedContent.find('.CustomOrder').remove();
                CompletedContent.find('.PagingMdPagingMd').remove();
                CompletedContent.Order(option);
                CompletedContent.PagingMd(PagingMdOption)

            }

        })
    }


    $(".Nav .SEO .SEOButton").on("click", function () {
        SEOVal = $(".Nav .SEO  input").val();
        CompletedOrder(1, SEOVal)
    });

    $(document).keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                $(".Nav .SEO .SEOButton").click();
                break;
        }
    });

});